<template>
  <div>
    <el-form
      ref="reform"
      v-loading="loading"
      :model="ruleForm"
      status-icon
      :rules="rules"
      :size="size"
      label-width="90px"
      :show-message="false"
      element-loading-background="rgba(255, 255, 255, 0.8)"
      element-loading-text="数据正在加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword" placeholder="请输入旧密码" show-password type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="ruleForm.newPassword" placeholder="请输入新密码" show-password type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" placeholder="请输入确认密码" show-password type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { validPassword } from '@/utils/validate'
import { editPassword } from '@/api/system/user'
import md5 from 'js-md5'

export default {
  props: {
    user: {
      type: Object,
      default: () => null
    }
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value.length < 6 || value.length > 20) {
        callback(new Error('密码长度在6-20位之间'))
      } else if (!validPassword(value)) {
        callback(new Error('密码格式不正确，仅支持字母、数字和下划线'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 加载开关
      loading: false,
      // 表单对象
      ruleForm: {},
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' },
          { required: true, validator: validatePassword, trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 布局大小
    size() {
      return this.$store.state.app.size
    }
  },
  methods: {
    // 保存
    submitForm() {
      this.$refs['reform'].validate((valid, obj) => {
        if (valid) {
          this.loading = true
          const form = {
            id: this.user.id,
            oldPassword: md5(this.ruleForm.oldPassword),
            newPassword: md5(this.ruleForm.newPassword)
          }
          editPassword(form).then(res => {
            this.$message.success('保存成功！')
            this.$store.dispatch('user/getInfo', true).finally(() => {
              this.loading = false
            })
          }).catch(() => {
            this.loading = false
          })
        } else {
          const field = []
          for (const key in obj) {
            field.push(obj[key][0].message)
          }
          this.$message.error(field[0])
          return false
        }
      })
    },
    // 重置
    resetForm() {
      this.ruleForm = { sex: 0 }
      this.$refs.reform.resetFields()
    }
  }
}
</script>
<style scoped>

</style>
